package progressBusiness;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.GetRequestInputStream;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Objects;

/**
 * Created by yangxianming on 2017/9/30.
 */
public class SaveProgress implements TestServerBusinessImpl {

    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig){
        //读取请求json
        JSONObject requestInfo = GetRequestInputStream.getJson(request);
        ReturnJson returnJson = new ReturnJson();

        //数据库操作
        System.out.println(requestInfo);
        String action = requestInfo.getString("action");

        //新建条目
        if (Objects.equals("newProgress", action)){
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            String insertSql = "INSERT INTO `progress` (`progressname`,`replay`,`detail`,`isfile`) VALUES ('','','[]','2')";
            SqlReturnModel sqlReturnModel = sqlDispatch.insert(insertSql);
            if (sqlReturnModel.getIsSuccess()){
                String selectSql = "SELECT id FROM progress ORDER BY id DESC LIMIT 1";
                SqlReturnModel sqlReturnModel1 = sqlDispatch.selectWord(selectSql);
                returnJson.setResult(sqlReturnModel1.getSelectReturnData().get(0).get(0));
                returnJson.setApiStatus(1);
            }

        }

        //save 更新项目进度
        if (Objects.equals("update", action)){
            String id = requestInfo.getString("id");
            String progressName = requestInfo.getString("progressName");
            String detail = requestInfo.getString("detail");
            String updateSql = "UPDATE progress SET progressname='"+progressName+"',detail='"+detail+"' WHERE id='"+id+"'";
            System.out.println(updateSql);
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            SqlReturnModel sqlReturnModel = sqlDispatch.update(updateSql);
            returnJson.setApiStatus((sqlReturnModel.getIsSuccess()) ? 1 : 0);
            returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
        }

        //file 归档
        if (Objects.equals("file", action)){
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            String id = requestInfo.getString("id");
            String sql = "SELECT detail FROM progress WHERE id = '"+id+"'";
            SqlReturnModel sqlReturnModel = sqlDispatch.selectWord(sql);
            String isFileDetail = getIsfiledetail(JSONArray.parseArray(sqlReturnModel.getSelectReturnData().get(0).get(0)));

            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String logName = sdf1.format(new Date());
            String updateSql = "UPDATE progress SET isfile='1', endtime='"+logName+"', isfiledetail='"+isFileDetail+"' WHERE id='"+id+"'";
            System.out.println(updateSql);

            sqlReturnModel = sqlDispatch.update(updateSql);
            returnJson.setApiStatus((sqlReturnModel.getIsSuccess()) ? 1 : 0);
            returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
        }

        return returnJson.toString();
    }

    private String getIsfiledetail(JSONArray detail){
        //把detail的case和bug数据汇总后存入isfiledetail
        HashMap<String, String> modulesHash = new HashMap<String, String>();
        JSONArray content = new JSONArray();
        JSONArray modules = new JSONArray();
        int caseCount = 0;  //5
        int bugCount = 0;   //7
        for (Object one: detail){
            JSONArray oneArr = (JSONArray)one;
            modulesHash.put(oneArr.getString(0), "");
            content.add(oneArr.getString(1));
            int caseNum = 0;
            if (!Objects.equals(oneArr.getString(5), "")){
                caseNum = Integer.valueOf(oneArr.getString(5));
            }
            caseCount = caseCount + caseNum;
            int bugNum = 0;
            if (!Objects.equals(oneArr.getString(7), "")){
                bugNum = Integer.valueOf(oneArr.getString(7));
            }
            bugCount = bugCount + bugNum;
        }
        for (String key: modulesHash.keySet()){
            modules.add(key);
        }
        JSONObject isFileDetail = new JSONObject();
        isFileDetail.put("modules", modules);
        isFileDetail.put("content", content);
        isFileDetail.put("case", caseCount);
        isFileDetail.put("bug", bugCount);

        return isFileDetail.toJSONString();
    }
}
